import Mock from 'mockjs'
import data from './data.json'

const mocker = function() {

    function creartSuccessRes(data,code) {
        return {
            meta: {
                status: code||200,
                msg: '成功'
            },
            data: data
        }
    }
    
    function createErrorRes(data,code) {
        return {
            meta: {
                status: code||201,
                msg: '失败'
            },
            data: data
        }
    }

    //简单获取url中的查询参数，转变为对象(不支持a[]=1&a[]=2,和a=1&a=2之类的复杂参数)
    function getUrlParams(url){
        if(url)
        {
            url=decodeURI(url);
            var idx=url.indexOf('?');
            if(idx!=-1) {
                idx++;
                var len=url.length;
                var res={};
                while(idx<len)
                {
                    var idx_key=url.indexOf('=',idx);
                    if(idx_key==-1) idx_key=len;
                    var key=url.substring(idx,idx_key);
                    if(idx_key<len) {
                        var idx_val=url.indexOf('&',idx_key+1);
                        if(idx_val==-1) idx_val=len;
                        res[key]=url.substring(idx_key+1,idx_val);                        
                    }
                    else
                        res[key].val='';
                    idx=idx_val+1;
                }
                return res;
            }
        }
    }


    var menus=[
        {
            id: 100,
            name: '账目管理',
            icon: 'el-icon-menu',
            children: [{
                id: 101,
                name: '账目列表',
                icon: 'el-icon-menu',
                path: 'menu101',
                children: []
            },{
                id: 102,
                name: '账目统计',
                icon: 'el-icon-menu',
                path: 'menu102',
                children: []
            }]
        }
    ];

    Mock.Random.guid();    
    Mock.Random.cname();
    // 模拟登陆
    // Mock.mock(process.env.VUE_APP_API+'login',creartSuccessRes({
    //     cname: '@cname()',
    //     token: '$guid'
    // }));

    // 模拟获取菜单
    //Mock.mock(process.env.VUE_APP_API+'menus',creartSuccessRes(menus));

   
    //mock指定path和medthod到方法callback(queryParams,responseBodyObject)
    function mockPath(method,path,callback) {
        var reg=new RegExp(process.env.VUE_APP_API+path);
        Mock.mock(reg,method, options=>{
            if(callback) {
                var path_vars=reg.exec(options.url,);
                callback(path_vars,options.body);
            }
        });
    }

    function mockUrlParams(method,path,callback) {
        var reg=new RegExp(process.env.VUE_APP_API+path+'.*');
        Mock.mock(reg,method, options=>{
            if(callback) {
                var left=options.url.substring(process.env.VUE_APP_API.length+path.length);
                return callback(getUrlParams(left),options.body);
            }
        });
    }

    Mock.mock(new RegExp(process.env.VUE_APP_API+'kuaidi/\\d+'),data);



    console.log('mock init');

}();

export default mocker;

